package com.gscitysfy.cus.drain.modules.inspection.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cloudfrmwk.security.utils.SecurityUtils;
import com.gscitysfy.cus.drain.constants.RedisKeyConstant;
import com.gscitysfy.cus.drain.modules.inspection.entity.DrainInspectionMnJob;
import com.gscitysfy.cus.drain.modules.inspection.entity.DrainInspectionRlTrack;
import com.gscitysfy.cus.drain.modules.inspection.entity.DrainInspectionRlUser;
import com.gscitysfy.cus.drain.modules.inspection.mapper.DrainInspectionRlTrackMapper;
import com.gscitysfy.cus.drain.modules.inspection.service.IDrainInspectionMnJobService;
import com.gscitysfy.cus.drain.modules.inspection.service.IDrainInspectionRlTrackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gscitysfy.cus.drain.modules.inspection.service.IDrainInspectionRlUserService;
import com.gscitysfy.cus.drain.utils.DistanceUtil;
import com.gscitysfy.cus.drain.utils.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Administrator
 * @since 2022-10-14 15:00:46
 */
@Service
public class DrainInspectionRlTrackServiceImpl extends ServiceImpl<DrainInspectionRlTrackMapper, DrainInspectionRlTrack> implements IDrainInspectionRlTrackService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private IDrainInspectionMnJobService jobService;

    @Resource
    private IDrainInspectionRlUserService userService;

    /**
     * 轨迹点位上传
     * @param track
     * @return
     */
    @Override
    public Result<Boolean> uploadTrack(DrainInspectionRlTrack track) {
        track.setCmUserId(Long.parseLong(SecurityUtils.getPrincipal().getUserId()));
        track.setCmUserName(SecurityUtils.getPrincipal().getName());

        String key = RedisKeyConstant.JOB_TRACK + track.getCmJobId() + ":" + SecurityUtils.getPrincipal().getUserId();
        Map<Object, Object> history = redisUtil.hmget(key);

        if(Objects.nonNull(history) && history.size() > 0){
            DrainInspectionRlTrack historyTrack = JSON.parseObject(JSON.toJSONString(history), DrainInspectionRlTrack.class);
            //计算当前距离与上一次上传的点位的距离，如果小于10米，则不保存
            double distance = DistanceUtil.getDistance(Double.parseDouble(track.getCmTrackLat()), Double.parseDouble(track.getCmTrackLng()),
                    Double.parseDouble(historyTrack.getCmTrackLat()), Double.parseDouble(historyTrack.getCmTrackLng()));
            //如果距离大于10米，则保存此次上传的点位，否则不保存
            if(distance > 10){
                boolean result = this.save(track);
                //更新最后一次上传的点位
                redisUtil.hmset(key, JSON.parseObject(JSON.toJSONString(track), Map.class));
                return Result.content(result);
            }else {
                return Result.content(true);
            }
        }
        redisUtil.hmset(key, JSON.parseObject(JSON.toJSONString(track), Map.class));
        return Result.content(this.save(track));
    }

    /**
     * 获取巡检中用户位置信息
     * @return
     */
    @Override
    public Result<List<DrainInspectionRlTrack>> getInspectionPersonPoints() {
        LambdaQueryWrapper<DrainInspectionMnJob> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrainInspectionMnJob::getCmJobStatus, 2);
        List<DrainInspectionMnJob> jobs = jobService.list(queryWrapper);

        List<DrainInspectionRlTrack> tracks = new ArrayList<>(16);
        jobs.forEach(job -> {
            LambdaQueryWrapper<DrainInspectionRlUser> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(DrainInspectionRlUser::getCmPlanId, job.getCmPlanId());

            List<DrainInspectionRlUser> userList = userService.list(userLambdaQueryWrapper);

            userList.forEach(user -> {
               String key = RedisKeyConstant.JOB_TRACK + job.getCmKid() + ":" + user.getCmUserId();
                Map<Object, Object> item = redisUtil.hmget(key);
                if(Objects.nonNull(item)){
                    DrainInspectionRlTrack track = JSON.parseObject(JSON.toJSONString(item), DrainInspectionRlTrack.class);
                    tracks.add(track);
                }
            });
        });
        return Result.content(tracks);
    }



}
